<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@page import="com.translation.dao.Selector"%>
<%@page import="java.sql.PreparedStatement"%>
<%@page import="com.translation.dao.Batch"%>
<%@page import="java.sql.ResultSet"%>
<%@page import="java.sql.SQLException"%>
<%@page import="com.translation.dao.BaseDAO"%>
<%@page import="com.translation.util.Util"%>
<%@page import="com.translation.entity.Word"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.io.IOException"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Verb Edit</title>
</head>
<%!
private BaseDAO dao = new BaseDAO();
%>
<body>
<%
        final String word_id = Util.toNull(request.getParameter("word_id") );
        final String locale_id = Util.toNull((String) session.getAttribute("locale_id"));
        final String submit = Util.toNull(request.getParameter("submit") );
        final Word word = new Word(word_id);
        request.setAttribute("word", word);
        
        ArrayList<String> session_types = (ArrayList<String>) session.getAttribute("session_types");
        final ArrayList<String> types = (session_types != null)? session_types: new ArrayList<String>();
        if (session_types == null)
        {
            Selector selector = new Selector() {
                public void setParams(PreparedStatement st) throws SQLException {
                }
                
                public void row(ResultSet rs) throws SQLException {
                    String type_id = rs.getString("type_id");
                    types.add(type_id);
                }
            };
            dao.selectQuery("type_ids.sql", selector);
            session.setAttribute("session_types",types);
        }
        
        ArrayList<String> session_class_ids = (ArrayList<String>) session.getAttribute("session_class_ids");
        final ArrayList<String> class_ids = (session_class_ids != null)? session_class_ids: new ArrayList<String>();
        if (session_class_ids == null)
        {
            Selector selector = new Selector() {
                public void setParams(PreparedStatement st) throws SQLException,IOException {
                }
                
                public void row(ResultSet rs) throws SQLException {
                    String class_id = rs.getString("class_id");
                    class_ids.add(class_id);
                }
            };
            dao.selectQuery("class_ids.sql", selector);
            session.setAttribute("session_class_ids",class_ids);
        }
        
        
        if (word_id != null) {
            Selector selector = new Selector() {
                public void setParams(PreparedStatement st) throws SQLException,IOException {
                    int index = 1;
                    st.setString(index++, word_id);
                }
                
                public void row(ResultSet rs) throws SQLException {
                    word.word = rs.getString("word");
                	word.token = rs.getString("token");
                	word.type_id = rs.getString("type_id");
                	word.class_id = rs.getString("class_id");
                	word.locale_id = rs.getString("locale_id");
                }
            };
            dao.selectQuery("word_byid.sql", selector);
        }
        
        if (submit != null) {
            final HttpServletRequest req = request;
            
            Batch batch = new Batch() {
                @Override
                public void setParams(PreparedStatement st) throws SQLException,IOException {
                    int index = 1;
                    st.setString(index++,req.getParameter("token"));
                    st.setString(index++,req.getParameter("word"));
                    st.setString(index++,req.getParameter("locale_id"));
                    st.setString(index++,req.getParameter("type_id"));
                    st.setString(index++,req.getParameter("pronunciation"));
                    if (word_id != null){
                        st.setString(index++,word_id);
                    }
                }

                @Override
                public boolean result(int[] result, PreparedStatement st) throws SQLException,IOException {
                    return result != null && result.length==1;
                }

                @Override
                public boolean next() throws SQLException,IOException {
                    return true;
                }

                @Override
                public int getSize() {
                    return 1;
                }

                @Override
                public void error(SQLException e) {
                   e.printStackTrace();
                }                
            };
            
            dao.namedBatch((word_id != null)? "word_save": "word_insert", batch);
        }
%>
    <form action="verb_edit.jsp">
        <table>
            <tr>
                <td>Word Id: </td>
                <td>
                    <input type="text" size="40" name="word_id" value="${word.word_id}"/>
                </td>
            </tr>
            <tr>
                <td>Word: </td>
                <td>
                    <input type="text" size="40" name="word" value="${word.word}"/>
                </td>
            </tr>
            <tr>
                <td>Token: </td>
                <td>
                    <input type="text" size="40" name="token" value="${word.token}"/>
                </td>
            </tr>
            <tr>
                <td>Pronunciation: </td>
                <td>
                    <input type="text" size="40" name="pronunciation" value="${word.pronunciation}"/>
                </td>
            </tr>
            <tr>
                <td>Type: </td>
                <td>
                    <select name="type_id">
                        <option></option>
                        <%
                            for(String type: types) {
                             %>
                                <option value="<%=type%>"<%= (type.equals(word.type_id)? "selected=\"selected\"":"") %>><%=type%></option>
                             <%   
                            }
                        %>
                    </select>                    
                </td>
            </tr>
            <tr>
                <td>Class: </td>
                <td>
                    <select name="class_id">
                        <option></option>
                        <%
                            for(String class_id: class_ids) {
                             %>
                                <option value="<%=class_id%>"<%= (class_id.equals(word.class_id)? "selected=\"selected\"":"") %>><%=class_id%></option>
                             <%   
                            }
                        %>
                    </select>
                </td>
            </tr>
            <tr>
                <td>Locale: </td>
                <td>
                    <select name="locale_id">
                        <option></option>
                        <option value="es"<%= ("es".equals(word.locale_id)? "selected=\"selected\"":"") %>>ES</option>
                        <option value="fr"<%= ("fr".equals(word.locale_id)? "selected=\"selected\"":"") %>>FR</option>
                        <option value="de" <%= ("de".equals(word.locale_id)? "selected=\"selected\"":"") %>>DE</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td>
                    <input type="submit" name="submit" value="Save" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>